package com.ssd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.api.product.ProductSkuApi;
import com.ssd.api.product.dto.SkuEntity;
import com.ssd.entity.ChannelEntity;
import com.ssd.entity.dto.ChannelConfigDto;
import com.ssd.mapper.ChannelDao;
import com.ssd.service.ChannelService;
import com.ssd.view.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelDao, ChannelEntity> implements ChannelService {

    @Autowired
    private ChannelDao channelDao;

    @Resource
    private ProductSkuApi productSkuApi;//远程调用接口


    /**
     * 分页查询商品列表，支持模糊查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return 分页的商品集合
     */
    @Override
    public Page<Map<String, Object>> getChannelList(Integer pageNum, Integer pageSize,String innerCode) {
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        return channelDao.selectChannel(page,innerCode);
    }

    /**
     * 修改售货机货道
     *
     */
    @Override
    public int updateChannel(ChannelConfigDto channelConfigDto) {
        //1. 将dto转为po对象
        List<ChannelEntity> channelList = channelConfigDto.getChannelList().stream().map(dto -> {
            ChannelEntity entity = new ChannelEntity();
            entity.setInnerCode(channelConfigDto.getInnerCode());
            entity.setChannelCode(dto.getChannelCode());
            entity.setSkuId(dto.getSkuId());
            // 设置其他必要的字段...
            return entity;
        }).collect(Collectors.toList());

        //2. 更新或保存实体
        return channelList.stream()
                .mapToInt(channelEntity -> {
                    // 根据业务逻辑选择更新或插入
                    // 这里假设有一个根据innerCode和channelCode查找的方法
                    ChannelEntity existingChannel = channelDao.selectOne(
                            new QueryWrapper<ChannelEntity>()
                                    .eq("inner_code", channelEntity.getInnerCode())
                                    .eq("channel_code", channelEntity.getChannelCode())
                    );
                    if (existingChannel != null) {
                        // 如果存在，则更新
                        channelEntity.setId(existingChannel.getId()); // 确保设置了主键ID
                        return channelDao.updateById(channelEntity);
                    } else {
                        // 如果不存在，则插入
                        return channelDao.insert(channelEntity);
                    }
                })
                .sum(); // 返回影响的行数总和
    }

    @Override
    public int batchInsertChannels(List<ChannelEntity> channelList) {
        return channelDao.batchInsertChannels(channelList);
    }

    @Override
    public List<ChannelEntity> getChannelList(String innerCode) {
        QueryWrapper<ChannelEntity> qw = Wrappers.query();
        qw.eq("inner_code",innerCode);
        List<ChannelEntity> list = this.list(qw);
        //获取商品信息
        for (ChannelEntity channelEntity : list) {
            BaseResponse<SkuEntity> skuById1 = productSkuApi.getSkuById1(channelEntity.getSkuId());
            channelEntity.setSku(skuById1.getData());
        }
        return list;
    }


}